﻿namespace Yom.WebService.Default
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using Yom.Common;
    using Yom.Template;

    public class DefaultList : BasePage
    {
        public string OutWrite()
        {
            string Type = Factory.Request("Type");
            string P = Factory.Request("P");
            int page = (string.IsNullOrEmpty(P) ? 1 : P.ToInt());
            int count = 0;
            string strP = string.Empty;
            List<Yom.Model.Comic> list = new List<Yom.Model.Comic>();
            int num = 0;
            SetCommon();
            SetRecommList();
            SetTopList();
            if (Regex.IsMatch(Type, @"^Top50$"))
            {
                SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- 最新更新50本");
                list = new Yom.Data.Comic().GetList("SELECT TOP 50 * FROM [Yom_Comic] ORDER BY [LastTime] DESC");
            }
            else if (Regex.IsMatch(Type, @"^0$"))
            {
                SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- 0-9");
                list = new Yom.Data.Comic().GetList(20, page, "[Initial] BETWEEN '0' AND '9'", false);
                count = new Yom.Data.Comic().GetCount("[Initial] BETWEEN '0' AND '9'");
                strP = "List/0/<{Page}>.ashx";
            }
            else if (Regex.IsMatch(Type, @"^[A-Z]$"))
            {
                SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- " + Type);
                list = new Yom.Data.Comic().GetList(20, page, "[Initial]='" + Type + "'", false);
                count = new Yom.Data.Comic().GetCount("[Initial]='" + Type + "'");
                strP = "List/" + Type + "/<{Page}>.ashx";
            }
            else
            {
                SkinTheme.SetLabel("PageTitle", SiteLoad.Scheme.SiteTitle + " -- 所有漫画");
                list = new Yom.Data.Comic().GetList(20, page, "", false);
                count = new Yom.Data.Comic().GetCount();
                strP = "List/<{Page}>.ashx";
            }

            if (list.Count > 0)
            {
                SkinTheme.SetBlock("List", list.Count);
                num = 0;
                foreach (Yom.Model.Comic model in list)
                {
                    SkinTheme.SetLabel(string.Format("List.SaveName_{0}", num), new Yom.Data.Category().GetModel(model.CategoryID).SaveName);
                    SkinTheme.SetLabel(string.Format("List.TID_{0}", num), Math.Ceiling(Convert.ToDouble(model.ComicID) / 1000).ToString());
                    SkinTheme.SetLabel(string.Format("List.CID_{0}", num), model.ComicID.ToString());
                    SkinTheme.SetLabel(string.Format("List.FaceExt_{0}", num), model.FaceExt);
                    SkinTheme.SetLabel(string.Format("List.Name_{0}", num), model.ComicName);
                    SkinTheme.SetLabel(string.Format("List.Visit_{0}", num), model.Visit.ToString());
                    SkinTheme.SetLabel(string.Format("List.LastName_{0}", num), new Yom.Data.Volume().GetLastVolume(model.ComicID).VolumeName);
                    SkinTheme.SetLabel(string.Format("List.State_{0}", num), new Yom.Data.Comic().GetState(model.State));
                    SkinTheme.SetLabel(string.Format("List.LastTime_{0}", num), model.LastTime.ToString("yyyy-mm-dd"));
                    num++;
                }

                if (Type != "Top50")
                {
                    string Multi = Factory.GetPager(20, page, count, strP);
                    SkinTheme.SetLabel("MultiPage", Multi.Replace("1.ashx", "Index.ashx"));
                }
            }

            return SkinTheme.RunParse("List.html");
        }
    }
}